
/**
 * 注册账号
 * @param username 账号
 * @param password 密码
 */
export async function register(username,password) {
    return await _database_.execute_insert(`
            INSERT INTO accounts (username, password, json_data) VALUES('${username}', '${password}', '');
    `);
}

/**
*  登录
* @param accounts 账号信息
*/
export function login(client_id,accounts) {
    //缓存账号信息
    _AccountsData.set(accounts["username"],accounts);
    //缓存客户端与账号的关联关系
    _ClientUserNameData.set(client_id,accounts["username"]);
    _ClientUidData.set(client_id,accounts["id"]);
    _UserNameClientData.set(accounts["username"],client_id);
    _UidClientData.set(accounts["id"],client_id);
    G.EventCallback.publish_event("账号登录",[client_id,accounts]);
}

/**
 * 判断账号是否存在
 * @param {*} username 
 * @returns 
 */
export async function is_exsit(username){
   let accounts = await query_accounts_db(username);
   return accounts.length > 0;
}

/**
 * 判断账号是否在线
 * @param {*} username 
 * @returns 
 */
export function is_online(username){
   let res = _AccountsData.get(username);
   return res !== undefined
}

/**
 * 根据用户名查询账号信息
 * @returns 
 */
export async function query_accounts_db(username) {
   return await _database_.execute_query_one(`
       select * from accounts where username = '${username}'
   `);
};


/**
* 删除当前账户的所有缓存信息
*/
export function clean_currrent_accounts_cache(client_id) {
    let username = _ClientUserNameData.get(client_id);
    if(username !== undefined){
        _ClientUserNameData.delete(client_id);
        _UserNameClientData.delete(username);
        _AccountsData.delete(username);
    }
    let uid = _ClientUidData.get(client_id);
    if(uid !== undefined){
        _UidClientData.delete(uid);
        _ClientUidData.delete(client_id)
    }
}